﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmartCollector.Domain.Repository;
using SmartCollector.Domain.Entity;
using HtmlParser;
using SmartCollector.Domain.Exceptions;
namespace SmartCollector.Controls
{
    public partial class AddLinkRss : Form
    {
        private LinkRssRepository m_linkRssRepository;
        private SiteRepository m_siteRepository;
        private List<CategoryHirarchy> m_listCategories;
        public AddLinkRss()
        {
            InitializeComponent();
        }

        public AddLinkRss(LinkRssRepository linkRepository,
            SiteRepository siteRepository, CategoryRepository cateRepository,int siteId):this()
        {
            m_linkRssRepository = linkRepository;
            m_siteRepository = siteRepository;
            m_listCategories = new List<CategoryHirarchy>(cateRepository.Categories.Count());
            BuildListCategoryHirarchy(cateRepository);

            m_cmbSites.DataSource = m_siteRepository.Sites.ToList();
            m_cmbSites.DisplayMember = "SiteName";

            m_cmbCategories.DataSource = m_listCategories;
            m_cmbCategories.DisplayMember = "CategoryName";
            tbl_ReferenceSite selectedSite = m_siteRepository.Sites.Where(p => p.ID == siteId).FirstOrDefault();
            if (selectedSite != null)
            {
                m_cmbSites.SelectedItem = selectedSite;
            }

        }

        private void BuildListCategoryHirarchy(CategoryRepository cateRepository)
        {
            List<tbl_Category> parentCategory= 
                cateRepository.Categories.Where(p=>!p.ParentID.HasValue).ToList();
            foreach (var item in parentCategory)
            {
                m_listCategories.Add(
                    new CategoryHirarchy(item.ID, item.CategoryName,null));
                var childCategory = cateRepository.Categories
                    .Where(p => p.ParentID == item.ID).ToList();
                foreach (var child in childCategory)
                {
                    m_listCategories.Add(new CategoryHirarchy(child.ID, child.CategoryName, item.ID));
                }
            }
        }

        private void AddLinkRssLoad(object sender, EventArgs e)
        {
           
        }

        private void OnSaveNewLinkRss(object sender, EventArgs e)
        {
            if (!m_txtLinkRss.Text.Trim().IsInternetLink())
            {
                MessageBox.Show("Link is not correct!");
                return;
            }
            var selectedSite = m_cmbSites.SelectedItem as tbl_ReferenceSite;
            var selectedCategory = m_cmbCategories.SelectedItem as CategoryHirarchy;
            if (selectedSite != null && selectedCategory != null)
            {
                try
                {
                    m_linkRssRepository.AddNewLinkRss(
                        m_txtLinkRss.Text.Trim(), selectedSite.ID, selectedCategory.Id);
                }
                catch (LinkRssAlreadyExistException ex)
                {
                    MessageBox.Show(string.Format(
                        "Already LinkRss for Category: {0} of Site {1}",
                        ex.CategoryName,ex.SiteName));
                    return;
                }
                m_linkRssRepository.SubmitChanged();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void OnCancelClick(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
    }

    internal class CategoryHirarchy
    {
        private string m_categoryName;
        public int Id
        {
            get;
            private set;
        }
        public int? ParentId
        {
            get;
            private set;
        }
        public string CategoryName
        {
            get
            {
                if (ParentId.HasValue)
                {
                    return string.Format("      |--{0}", m_categoryName);
                }
                return "+ " + m_categoryName;
            }

        }

        public CategoryHirarchy(int id, string name, int? parentId)
        {
            Id = id;
            m_categoryName = name;
            ParentId = parentId;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public enum EditMode
    {
        None,
        Edit,
        Add,
        Remove
    }
}
